package extraction.html;

import org.htmlparser.Node;

import java.util.List;

/**
 * @author Nathan Petryk (nathanp@indeed.com)
 */
public class ExtractionUtils {
    public static int findWith(List<Node> list, String contains, Node... nodes) {
        int cnt = 0;
        for(Node n : nodes) {

            if(n == null) continue;
            if(n.getText().contains(contains)) {
                list.add(n);
                cnt++;
            }

            if(n.getChildren() == null) continue;
            cnt += findWith(list, contains, n.getChildren().toNodeArray());
        }
        return cnt;
    }

    public static int getNodesOfType(String type, int maxDepth, Node start, List<Node> depository) {
        int cnt = 0;

        String txt = start.getText();
        int i = txt.indexOf(" ");
        if( txt.startsWith(type) ) {
            depository.add(start);
            cnt++;
        }

        if(start.getChildren() != null && maxDepth > 0 && cnt == 0)
        for(Node n : start.getChildren().toNodeArray()) {
            getNodesOfType(type, maxDepth-1, n, depository);
        }

        return cnt;
    }

    public static void printNodes(Node node, String prefix) {
        if(node == null) return;

        System.out.println(prefix+node.getText());

        if(node.getChildren() == null) return;
        for(Node n : node.getChildren().toNodeArray()) {
            printNodes(n, prefix + prefix.charAt(0));
        }
    }
}
